//
//  Commen.m
//  Pink
//
//  Created by 杨成阳 on 15/12/6.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "Commen.h"

@implementation Commen

+(NSMutableAttributedString *)parseTheContentWithContent:(NSArray *)content font:(UIFont *)font color:(UIColor *)color{
    
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
    text.font = font;
    text.color = color;
    for (int i = 0; i < content.count; i ++) {
        
        NSDictionary *item = content[i];
        if ([item[@"content"] length]>0) {
            [text appendString:item[@"content"]];
        }
        //图片
        if ([item[@"imagePath"] length]>0) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 200)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [imageView setImageWithURL:[NSURL URLWithString:item[@"imagePath"]] placeholder:nil options:YYWebImageOptionProgressiveBlur | YYWebImageOptionShowNetworkActivity | YYWebImageOptionSetImageWithFadeAnimation completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
            }];
            NSMutableAttributedString *imageString = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            
            [imageString insertString:@"\n\n" atIndex:0];
            [imageString appendString:@"\n\n"];
            [text appendAttributedString:imageString];
        }
        //视频
        if (item[@"videoPath"]) {
            
        }
        
    }
    
    return text;
}


+ (void)setDoneButtonWith:(YYTextView *)textView
{
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 32)];
    toolView.backgroundColor = [UIColor clearColor];
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneButton setImage:[UIImage imageNamed:@"hideKeyBoard"] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    doneButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 40, 0, 40, 32);
    [toolView addSubview:doneButton];
    
     [textView setInputAccessoryView:toolView];
}

+ (NSString *)simpleDateWithDate:(NSDate *)date
{
    // 1.获得年月日
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:date];
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if ([cmp1 day] == [cmp2 day] && [cmp1 month] == [cmp2 month]) { // 今天
        formatter.dateFormat = @"HH:mm";
    } else if ([cmp1 year] == [cmp2 year]) { // 今年
        formatter.dateFormat = @"MM-dd HH:mm";
    } else {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    NSString *time = [formatter stringFromDate:date];
    
    return time;
}


+(void)dismissKeyBoard
{
    [[NSNotificationCenter defaultCenter] postNotificationName:YYTEXTVIEWHIDEKEYBOARDNOTIFICATION object:nil];
}


@end
